﻿Imports System.Data
Imports MyGeneration.dOOdads

Public Class rc_Materiales

    Private _MessageError As Exception = Nothing
    Public Property MessageError() As Exception
        Get
            Return _MessageError
        End Get
        Set(value As Exception)
            _MessageError = value
        End Set
    End Property


#Region "Materiales"

    Public Function Insert_Materiales(conn As String, codTipoMaterial As Integer, descMaterial As String, cantExistente As Integer,
                                      costoMaterial As Decimal, cantMinima As Integer, cantMaxima As Integer,
                                      codUsuario As Integer) As Integer

        Dim tx As TransactionMgr
        tx = TransactionMgr.ThreadTransactionMgr()

        Try
            tx.BeginTransaction()

            Dim mat As New Class_RC_Data.Class_RC.MATERIALES(conn)

            If mat.Where.DescMaterial.Value <> descMaterial Then
                mat.AddNew()
                mat.CodTipoMaterial = codTipoMaterial
                mat.DescMaterial = descMaterial
                mat.CantExistente = cantExistente
                mat.CantMinima = cantMinima
                mat.CantMaxima = cantMaxima
                mat.CostoMaterial = costoMaterial
                'mat.CodProveedor = codProveedor
                mat.CodUsuario = codUsuario
                mat.FechaActualizacion = DateTime.Now
                mat.Save()


            End If

            tx.CommitTransaction()

            Return mat.CodMaterial
        Catch ex As Exception
            tx.RollbackTransaction()
            MessageError = ex
            Return 0
        End Try

    End Function

    Public Function Update_Materiales(conn As String, codMaterial As Integer,
                                     codTipoMaterial As Integer, descMaterial As String, cantExistente As Integer,
                                      costoMaterial As Decimal, cantMinima As Integer, cantMaxima As Integer,
                                      codUsuario As Integer) As Boolean

        Dim tx As TransactionMgr
        tx = TransactionMgr.ThreadTransactionMgr()

        Try
            tx.BeginTransaction()

            Dim mat As New Class_RC_Data.Class_RC.MATERIALES(conn)
            mat.Where.CodMaterial.Value = codMaterial

            If mat.Query.Load Then

                mat.CodTipoMaterial = codTipoMaterial
                mat.DescMaterial = descMaterial
                mat.CantExistente = cantExistente
                mat.CantMinima = cantMinima
                mat.CantMaxima = cantMaxima
                mat.CostoMaterial = costoMaterial
                'mat.CodProveedor = codProveedor
                mat.CodUsuario = codUsuario
                mat.FechaActualizacion = DateTime.Now
                mat.Save()

            End If

            tx.CommitTransaction()
            Return True

        Catch ex As Exception
            tx.RollbackTransaction()
            MessageError = ex
            Return False
        End Try

    End Function

    Public Function Delete_Materiales(conn As String, codMaterial As Integer) As Boolean

        Dim tx As TransactionMgr
        tx = TransactionMgr.ThreadTransactionMgr()

        Try
            tx.BeginTransaction()

            Dim mat As New Class_RC_Data.Class_RC.MATERIALES(conn)
            mat.Where.CodMaterial.Value = codMaterial

            If mat.Query.Load Then

                mat.MarkAsDeleted()
                mat.Save()

            End If

            tx.CommitTransaction()
            Return True

        Catch ex As Exception
            tx.RollbackTransaction()
            MessageError = ex
            Return False
        End Try

    End Function

    Public Function Control_Materiales_Pedidos(conn As String, codMaterial As Integer) As Boolean
        Try
            Dim venta As New Class_RC_Data.Class_RC.VistaNotasDetalleMaterial(conn)
            venta.Where.CodMaterial.Value = codMaterial
            If venta.Query.Load Then

                For i As Integer = 0 To venta.DefaultView.Table.Rows.Count - 1

                    Dim ventaCab As New Class_RC_Data.Class_RC.VistaNotas(conn)
                    ventaCab.Where.CodVenta.Value = venta.DefaultView.Table.Rows(i)("CodNota")
                    ventaCab.Where.DescEstadoNota.Value = "ANULADO"
                    ventaCab.Where.DescEstadoNota.Operator = WhereParameter.Operand.Like
                    If ventaCab.Query.Load Then
                        Return True
                    End If

                Next

                Return True

            End If

            Return False

        Catch ex As Exception
            MessageError = ex
            Return False
        End Try
    End Function

    Public Function get_Materiales_All(conn As String, codMaterial As Integer, descripcion As String, codTipoMaterial As Integer) As DataTable
        Try

            Dim vMat As New Class_RC_Data.Class_RC.VistaMateriales(conn)

            If codMaterial > 0 Then
                vMat.Where.CodMaterial.Value = codMaterial
            End If

            If descripcion <> "" Then
                vMat.Where.DescMaterial.Value = "%" & descripcion & "%"
                vMat.Where.DescMaterial.Operator = WhereParameter.Operand.Like
            End If

            If codTipoMaterial > 0 Then
                vMat.Where.CodTipoMaterial.Value = codTipoMaterial
            End If

            'If codProveedor > 0 Then
            '    vMat.Where.CodProveedor.Value = codProveedor
            'End If

            vMat.Query.AddOrderBy("descTipoMaterial", WhereParameter.Dir.ASC)
            vMat.Query.AddOrderBy("descMaterial", WhereParameter.Dir.ASC)

            If vMat.Query.Load Then
                If vMat.DefaultView.Table.Rows.Count > 0 Then
                    Return vMat.DefaultView.Table
                Else
                    Return Nothing
                End If

            Else
                Return Nothing

            End If

        Catch ex As Exception
            MessageError = ex
            Return Nothing
        End Try

    End Function

    Public Function get_tipo_Materiales(conn As String) As DataTable
        Try

            Dim vTipo As New Class_RC_Data.Class_RC.TIPOMATERIAL(conn)

            vTipo.Query.AddOrderBy("descTipoMaterial", WhereParameter.Dir.ASC)

            If vTipo.Query.Load Then
                If vTipo.DefaultView.Table.Rows.Count > 0 Then
                    Return vTipo.DefaultView.Table
                Else
                    Return Nothing
                End If

            Else
                Return Nothing

            End If

        Catch ex As Exception
            MessageError = ex
            Return Nothing
        End Try

    End Function

    Public Function get_tipo_unidMedida(conn As String) As DataTable
        Try

            Dim vTipoUni As New Class_RC_Data.Class_RC.UNIMEDIDA(conn)

            If vTipoUni.Query.Load Then
                If vTipoUni.DefaultView.Table.Rows.Count > 0 Then
                    Return vTipoUni.DefaultView.Table
                Else
                    Return Nothing
                End If

            Else
                Return Nothing

            End If

        Catch ex As Exception
            MessageError = ex
            Return Nothing
        End Try

    End Function

    Public Function get_proveedores(conn As String) As DataTable
        Try

            Dim vProv As New Class_RC_Data.Class_RC.PROVEEDORES(conn)

            vProv.Query.AddOrderBy("nombre", WhereParameter.Dir.ASC)

            If vProv.Query.Load Then
                If vProv.DefaultView.Table.Rows.Count > 0 Then
                    Return vProv.DefaultView.Table
                Else
                    Return Nothing
                End If

            Else
                Return Nothing

            End If

        Catch ex As Exception
            MessageError = ex
            Return Nothing
        End Try

    End Function

    Public Function get_Materiales(conn As String, codMaterial As Integer) As DataTable
        Try

            Dim vMateriales As New Class_RC_Data.Class_RC.MATERIALES(conn)

            If codMaterial > 0 Then
                vMateriales.Where.CodMaterial.Value = codMaterial
            End If

            vMateriales.Query.AddOrderBy("DescMaterial", WhereParameter.Dir.ASC)

            If vMateriales.Query.Load Then
                If vMateriales.DefaultView.Table.Rows.Count > 0 Then
                    Return vMateriales.DefaultView.Table
                Else
                    Return Nothing
                End If

            Else
                Return Nothing

            End If

        Catch ex As Exception
            MessageError = ex
            Return Nothing
        End Try

    End Function

    Public Function Insert_Materiales_Detalle(conn As String, codMaterial As Integer, cantUniMed As Decimal, costoUnidad As Decimal,
                                              cantExistente As Decimal, largoMat As Decimal, anchoMat As Decimal) As Boolean

        Dim tx As TransactionMgr
        tx = TransactionMgr.ThreadTransactionMgr()

        Try
            tx.BeginTransaction()

            Dim detMat As New Class_RC_Data.Class_RC.DETALLEMATERIAL(conn)

            detMat.AddNew()
            detMat.CodMaterial = codMaterial
            detMat.CantUnidadMaterial = cantUniMed
            detMat.CostoUnidad = costoUnidad
            detMat.CantExistente = cantExistente
            detMat.LargoMaterial = largoMat
            detMat.AnchoMaterial = anchoMat
            detMat.Save()

            tx.CommitTransaction()

            Return True

        Catch ex As Exception
            tx.RollbackTransaction()
            MessageError = ex
            Return False
        End Try

    End Function

    Public Function Update_Materiales_Detalle(conn As String, codMaterial As Integer, cantUniMed As Decimal, costoUnidad As Decimal,
                                              cantExistente As Decimal, largoMat As Decimal, anchoMat As Decimal) As Boolean

        Dim tx As TransactionMgr
        tx = TransactionMgr.ThreadTransactionMgr()

        Try
            tx.BeginTransaction()

            Dim detMat As New Class_RC_Data.Class_RC.DETALLEMATERIAL(conn)
            detMat.Where.CodMaterial.Value = codMaterial
            If detMat.Query.Load Then
                detMat.CantUnidadMaterial = cantUniMed
                detMat.CostoUnidad = costoUnidad
                detMat.CantExistente = cantExistente
                detMat.LargoMaterial = largoMat
                detMat.AnchoMaterial = anchoMat
                detMat.Save()

            End If

            tx.CommitTransaction()
            Return True

        Catch ex As Exception
            tx.RollbackTransaction()
            MessageError = ex
            Return False
        End Try

    End Function

    Public Function Delete_Materiales_Detalle(conn As String, codDetalleMaterial As Integer) As Boolean

        Dim tx As TransactionMgr
        tx = TransactionMgr.ThreadTransactionMgr()

        Try
            tx.BeginTransaction()

            Dim detMat As New Class_RC_Data.Class_RC.DETALLEMATERIAL(conn)

            detMat.Where.CodDetalleMaterial.Value = codDetalleMaterial
            If detMat.Query.Load Then
                detMat.MarkAsDeleted()
                detMat.Save()
            End If

            tx.CommitTransaction()

            Return True

        Catch ex As Exception
            tx.RollbackTransaction()
            MessageError = ex
            Return False
        End Try

    End Function

    Public Function get_Materiales_Detalles_All(conn As String, codMaterial As Integer) As DataTable
        Try

            Dim vDetMat As New Class_RC_Data.Class_RC.VistaMaterialesDetalle(conn)

            If codMaterial > 0 Then
                vDetMat.Where.CodMaterial.Value = codMaterial
            End If


            If vDetMat.Query.Load Then
                If vDetMat.DefaultView.Table.Rows.Count > 0 Then
                    Return vDetMat.DefaultView.Table
                Else
                    Return Nothing
                End If

            Else
                Return Nothing

            End If

        Catch ex As Exception
            MessageError = ex
            Return Nothing
        End Try

    End Function

#End Region

End Class
